package com.tangly.codeing.controller;

import com.tangly.codeing.model.SysRole;
import com.tangly.codeing.service.SysPrivilegeService;
import com.tangly.codeing.service.SysRolePrivilegeService;
import com.tangly.codeing.service.SysRoleService;
import com.tangly.codeing.utils.PageInfo;
import com.tangly.codeing.utils.ReturnT;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 角色表
 *
 * @author tangbing
 * @date 2019-05-05
 */
@RestController
@RequestMapping("sysRole")
public class SysRoleController extends BaseController {

    @Resource
    private SysRoleService sysRoleService;
    @Resource
    private SysPrivilegeService sysPrivilegeService;
    @Resource
    private SysRolePrivilegeService sysRolePrivilegeService;

    /**
     * 新增
     */
    @PostMapping("/insert")
    public ReturnT<String> insert(@RequestBody SysRole sysRole) {
        return sysRoleService.insert(sysRole);
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public ReturnT<String> delete(@PathVariable("id") int id) {
        return sysRoleService.delete(id);
    }

    /**
     * 批量删除
     */
    @GetMapping("/batchDelete")
    public ReturnT<String> batchDelete(@RequestParam("ids") String ids) {
        String[] split = ids.split(",");
        return sysRoleService.batchDelete(Arrays.asList(split));
    }

    /**
     * 更新
     */
    @PostMapping("/update")
    public ReturnT<String> update(@RequestBody SysRole sysRole) {
        return sysRoleService.update(sysRole);
    }

    /**
     * Load查询
     */
    @GetMapping("/load/{id}")
    public ReturnT<Object> load(@PathVariable("id") int id) {
        return new ReturnT<>(sysRoleService.load(id));
    }

    /**
     * 分页查询
     */
    @PostMapping("/pageList")
    public ReturnT<Object> pageList(@RequestBody PageInfo pageInfo) {
        ReturnT<Object> validateRes = validateParam(pageInfo);
        if (validateRes != null) {
            return validateRes;
        }
        return new ReturnT<>(sysRoleService.pageList(pageInfo));
    }

    /**
     * 根据用户id获取角色
     */
    @GetMapping("/getByUserId/{userId}")
    public ReturnT<Object> getByUserId(@PathVariable("userId") int userId) {
        Map<String, Object> map = new HashMap<>(2);
        List<SysRole> roleList = sysRoleService.getByUserId(userId);
        // 用户角色
        map.put("userRole", roleList);
        // 所有角色
        map.put("allRole", sysRoleService.findAll());
        // 用户角色id
        map.put("userRoleId", roleList.stream().map(SysRole::getId).collect(Collectors.toList()));
        return new ReturnT<>(map);
    }

    /**
     * 根据用户id获取角色
     */
    @GetMapping("/findAll")
    public ReturnT<Object> findAll() {
        return new ReturnT<>(sysRoleService.findAll());
    }

    /**
     * 获取所有资源权限
     */
    @GetMapping("/getAllPrivilege/{roleId}")
    public ReturnT<Object> getAllPrivilege(@PathVariable("roleId") String roleId) {
        Map<String, Object> data = new HashMap<>(2);
        data.put("systemPermissions", sysPrivilegeService.findAllRoot());
        data.put("assignedPermissions", sysPrivilegeService.findPrivilegeIdByRoleId(roleId));
        return new ReturnT<>(data);
    }
}
